Email subscription checks and form layout

jamesperet преди 9 години
родител
ревизия
be7c49b9ea
променени са 4 файла, в които са добавени 43 реда и са изтрити 32 реда
  1. 13 7
      app/controllers/subscription_controller.rb
  2. 19 14
      app/views/layouts/_subscribe_to_newsletter.html.erb
  3. 2 3
      app/views/start/index.html.erb
  4. 9 8
      features/email_subscription.feature

+ 13 - 7
app/controllers/subscription_controller.rb

@@ -1,15 +1,21 @@
1 1
 class SubscriptionController < ApplicationController
2
+  
2 3
   def create
3 4
     @subscription = Subscription.new(subscription_params)
4 5
     respond_to do |format|
5
-      if @subscription.save
6
-        UserMailer.newsletter_subscription(@subscription).deliver 
7
-        format.html { redirect_to root_path, notice: 'Thanks for subscribing to our newsletter' }
8
-        format.json { render action: 'show', status: :created, location: @subscription }
6
+      if Subscription.find_by_email(@subscription.email) == nil 
7
+        if @subscription.save
8
+          UserMailer.newsletter_subscription(@subscription).deliver 
9
+          format.html { redirect_to root_path, notice: 'Thanks for subscribing to our newsletter' }
10
+          format.json { render action: 'show', status: :created, location: @subscription }
11
+        else
12
+          format.html { redirect_to root_path, alert: 'An error ocured. Please try gain.' }
13
+          format.json { render json: @subscription.errors, status: :unprocessable_entity }
14
+        end     
9 15
       else
10
-        format.html { redirect_to root_path, alert: 'An error ocured. Please try gain.' }
11
-        format.json { render json: @subscription.errors, status: :unprocessable_entity }
12
-      end
16
+        format.html { redirect_to root_path, alert: 'You have already registered to our newsletter' }
17
+        format.json { render action: 'show', status: :created, location: @subscription }
18
+      end      
13 19
     end
14 20
   end
15 21
   

+ 19 - 14
app/views/layouts/_subscribe_to_newsletter.html.erb

@@ -1,14 +1,19 @@
1
-<h3><%= (t 'subscription.header')%></h3>
2
-<%= bootstrap_form_for(@subscription) do |f| %>
3
-  <%= f.alert_message "Please fix the errors below."%>
4
-  <%= f.form_group :first_name do %>
5
-    	<%= f.text_field :first_name, label: (t 'registration.first_name'), class: 'input-block-level', required: true %>
6
-  <% end %>
7
-  <%= f.form_group :last_name do %>
8
-    	<%= f.text_field :last_name, label: (t 'registration.last_name'), class: 'input-block-level', required: true %>
9
-  <% end %>
10
-  <%= f.form_group :email do %>
11
-    	<%= f.text_field :email, label: (t 'registration.email'), class: 'input-block-level', required: true %>
12
-  <% end %>
13
-  <%= f.submit (t 'subscription.submit'), class: 'btn btn-success', id: 'submit_subscription' %>
14
-<% end %>
1
+
2
+<li class="span3">
3
+	<div class="thumbnail" style="margin-left: 0px; padding: 10px; padding-top: 0px; height: 300px;">
4
+		<h4><%= (t 'subscription.header')%></h4>
5
+		<%= bootstrap_form_for(@subscription) do |f| %>
6
+		  <%= f.alert_message "Please fix the errors below."%>
7
+		  <%= f.form_group :first_name do %>
8
+		    	<%= f.text_field :first_name, label: (t 'registration.first_name'), class: 'input-block-level', required: true %>
9
+		  <% end %>
10
+		  <%= f.form_group :last_name do %>
11
+		    	<%= f.text_field :last_name, label: (t 'registration.last_name'), class: 'input-block-level', required: true %>
12
+		  <% end %>
13
+		  <%= f.form_group :email do %>
14
+		    	<%= f.text_field :email, label: (t 'registration.email'), class: 'input-block-level', required: true %>
15
+		  <% end %>
16
+		  <%= f.submit (t 'subscription.submit'), class: 'btn btn-success pull-right', id: 'submit_subscription' %>
17
+		<% end %>
18
+	</div>
19
+</li>

+ 2 - 3
app/views/start/index.html.erb

@@ -21,8 +21,7 @@
21 21
 	     </div>
22 22
 	   </li>
23 23
     <% end %>
24
+    <%= render 'layouts/subscribe_to_newsletter' %>
24 25
 </ul>
25
-
26
-
27 26
 <br>
28
-<%= render 'layouts/subscribe_to_newsletter' %>
27
+

+ 9 - 8
features/email_subscription.feature

@@ -32,19 +32,20 @@ Feature: Email Subscription
32 32
 		And I should see "John Doe"
33 33
 		And I should see "john_doe@website.com"
34 34
 		
35
+	Scenario: Somebody already registered tries to submit to newsletter subscription form
36
+		Given I visit the homepage
37
+		When I fill in "subscription_first_name" with "Jimy"
38
+		And I fill in "subscription_last_name" with "San"
39
+		And I fill in "subscription_email" with "jimysan@website.com"
40
+		And I click in the button "submit_subscription"
41
+		Then I should see "You have already registered to our newsletter"
42
+		
35 43
 	Scenario: Export subscription list as a CVS file
36 44
 		Given I am logged in as admin
37 45
 		And I go to the subscribers page
38 46
 		When I click in the link "Export CVS"
39 47
 		Then I should see "Jimy,San,jimysan@website.com,"
40 48
 		And I should see "John,Doe,john_doe@website.com"
41
-		
42
-		
43
-	# 1. Create subscription model
44
-	# 2. Create subscription controller
45
-	# 3. Create subscription form view helper
46
-	# 4. Define subscribers method in admin controller
47
-	# 5. Create subscribers view
48
-	# 6. Define Export to CVS method in admin controller
49
+
49 50
 	# 7. Hookup the mailchimp API and send subscriber after subscription
50 51